/**
 * Copyright (c) 2016-2100 yunclouds
 */
package com.yunqian.web;

import com.yunqian.common.CurrentUser;
import com.yunqian.jwt.JwtUser;
import com.yunqian.redot.constant.Operation;
import com.yunqian.redot.constant.OperationMenus;
import com.yunqian.redot.domain.CoursesWords;
import com.yunqian.redot.domain.view.CoursesWordsDetails;
import com.yunqian.redot.service.CoursesWordsDetailsService;
import com.yunqian.redot.service.CoursesWordsService;
import com.yunqian.redot.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * Describe: 课程留言控制器
 *
 * @author zsy
 * 2018-09-29 15:54:17
 */
@RestController
@RequestMapping("/api/coursesWords")
public class CoursesWordsController {

    @Autowired
    private CoursesWordsService coursesWordsService;
    @Autowired
    private CoursesWordsDetailsService coursesWordsDetailsService;
    @Autowired
    private OperationLogService operationLogService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<CoursesWordsDetails> pagination(@RequestBody Map<String, Object> params) {
        return coursesWordsDetailsService.pagination(params);
    }

    /**
     * 设置精选
     *
     * @param id         待更新id
     * @param modifyData 待更新的数据
     * @return
     */
    @PutMapping("/{id}")
    public CoursesWords update(@PathVariable("id") Long id, @RequestBody Map<String, Object> modifyData, @CurrentUser JwtUser user) {
        CoursesWords words = coursesWordsService.update(id, modifyData);
        if (words.getAsBrilliant() == 1) {
            operationLogService.save(OperationMenus.SIGN_MESSAGE, Operation.SET_CHOICENESS, user.getId());
        } else {
            operationLogService.save(OperationMenus.SIGN_MESSAGE, Operation.CANCLE_CHOICENESS, user.getId());
        }
        return words;
    }

}
